package com.rc.web.controller;



import com.rc.pojo.vo.file.UploadVO;
import com.rc.web.service.FileService;
import com.rc.web.service.PosterService;
import com.rc.web.service.impl.PaintingService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/painting")
@CrossOrigin(origins = "*")  //这个注解是允许跨域请求的
public class PaintingController {
    @Autowired
    private PosterService posterService;

    @Autowired
    private PaintingService paintingService;

    @Autowired
    private  FileService fileService;

    /**
     *
     * @param user
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/run", produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> painting(@RequestParam String user) throws Exception {
        String prompt = "。根据以上满足用户的要求";
        String result = paintingService.task_submit(user,prompt);
        System.out.println("返回内容：" + result);
        
        // 将返回数据封装为JSON格式
        Map<String, Object> response = new HashMap<>();
        response.put("status", "success");
        response.put("data", result);
        return response;
    }

    /**
     *
     * @param text
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/look", produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> Look(@RequestParam String text) throws Exception {
        // text = "bf998bf0c48d57b5a8247851cfcd2875";
        String result = paintingService.taskProgress(text);
        System.out.println("返回内容：" + result);

        // 将返回数据封装为JSON格式
        Map<String, Object> response = new HashMap<>();
        response.put("status", "success");
        response.put("data", result);  // 使用data字段更通用
        return response;
    }

    /**
     * 根据用户需求直接返回图片
     * @param userInput 用户输入的需求
     * @return 图片数据
     * @throws Exception
     */

    @GetMapping(value = "/image")//, produces = MediaType.IMAGE_PNG_VALUE
    public String getImage(@RequestParam String userInput) throws Exception {

        String result = paintingService.generateImage(userInput);
        System.out.println("返回内容：" + result);

        return result;
    }


    @GetMapping(value = "/poster")
    public String getPoster(@RequestParam String content) throws Exception {

        String result = paintingService.generatePoster(content);
        System.out.println("返回内容：" + result);

        return result;
    }


    /**
     * 上传图片到MinIO服务器
     * @param imageUrl
     * @return
     * @throws Exception
     */
    @PostMapping("/save")
    public UploadVO saveImage(@RequestBody SaveImageRequest imageUrl) throws Exception {
        String url = imageUrl.getImageUrl();
        MultipartFile file = paintingService.downloadImage(url);
        // 使用新方法上传到指定文件夹
        UploadVO uploadVO = fileService.uploadToFolder(file, "rural-classroom-mini-program-master/static/poster");
        String newUrl = uploadVO.getUrl();
    
        // 保存图片
        posterService.saveImage(newUrl);
        return uploadVO;
    }

    

    // 定义请求体DTO
    @Data // Lombok注解，自动生成getter/setter
    @NoArgsConstructor
    @AllArgsConstructor
    public static class SaveImageRequest {
        private String imageUrl;
    }


    /**
     * 获取Poster表中的所有图片
     * @return 包含图片URL列表的响应
     */
    @GetMapping("/posters")
    public Map<String, Object> getAllPosterImages() {
        // 调用PosterService获取所有图片URL
        List<String> images = posterService.getAllImages();
        
        // 构建返回结果
        Map<String, Object> response = new HashMap<>();
        response.put("status", "success");
        response.put("data", images);
        return response;
    }

    /**
     * 获取指定文件夹中的所有图片
     * @return 包含图片URL列表的响应
     */
    @GetMapping("/folderImages")
    public Map<String, Object> getImagesFromFolder() {
        // 调用FileService获取指定文件夹中的图片URL
        String folderPath = "rural-classroom-mini-program-master/static/poster/";
        List<String> images = fileService.getImagesFromFolder(folderPath);
        
        // 构建返回结果
        Map<String, Object> response = new HashMap<>();
        response.put("status", "success");
        response.put("data", images);
        return response;
    }
}
